package Dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import Dao.Daomcfour;
import entity.mcfour;
import entity.mcthree;
import utils.BaseDao;

public class Daoimplmcfour extends BaseDao implements Daomcfour {

	@Override
	public List<mcfour> mcfourlist() {
		String sql = "select * from mcfour";
		ResultSet set = super.executeQuery(sql, null);
		List<mcfour> list = new ArrayList<mcfour>();
		try {
			while(set.next()){
				list.add(new mcfour(set.getInt(1),set.getString(2),set.getString(3),set.getInt(5),set.getFloat(4)));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			super.closeAll(set, null, null);
		}
		return list;
	}

	@Override
	public List<mcfour> mcfourlist(int spid) {
		String sql = "select * from mcfour where spid = ?";
		Object[] object = {spid};
		ResultSet set = super.executeQuery(sql, object);
		List<mcfour> list = new ArrayList<mcfour>();
		try {
			while(set.next()){
				list.add(new mcfour(set.getInt(1),set.getString(2),set.getString(3),set.getInt(5),set.getFloat(4)));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			super.closeAll(set, null, null);
		}
		return list;
	}

	@Override
	public List<mcfour> mcfourlist(int offset, int pagesize,int spid) {
		String sql = "select * from mcfour where spid = ? limit ?,? ";
		Object[] object = {spid,offset,pagesize};
		ResultSet set = super.executeQuery(sql, object);
		List<mcfour> list = new ArrayList<mcfour>();
		try {
			while(set.next()){
				list.add(new mcfour(set.getInt(1),set.getString(2),set.getString(3),set.getInt(5),set.getFloat(4)));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			super.closeAll(set, null, null);
		}
		return list;
	}

	@Override
	public int count(int spid) {
		String sql = "select count(*) from mcfour where spid = ?";
		Object[] object = {spid};
		ResultSet set = super.executeQuery(sql, object);
		int count = 0;
		try {
			while(set.next()){
				count = set.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			super.closeAll(set, null, null);
			
		}
		return count;
	}

	@Override
	public boolean add(mcfour mc) {
		String sql = "insert into `project`.`mcfour` (`name`, `img`, `money`, `spid`) values (?,?,?,?)";
		Object[] object = {mc.getName(),mc.getImg(),mc.getMoney(),mc.getSpid()};
		return super.executeUpdate(sql, object);
	}

	@Override
	public boolean del(int id) {
		String sql = "delete from `project`.`mcfour` where `id` = ?";
		Object[] object = {id};
		return super.executeUpdate(sql, object);
	}

}
